import React from 'react';

import { Declare } from '@/pages/RecruitEmail/pc/components';

// needBackgroundFlag 是否需要背调
const DeclareInfo = ({ needBackgroundFlag, form, id, title, file, isMobile, disabled }) => {
    const { getFieldDecorator } = form;

    const getFileType = fileName => {
        if (fileName) {
            const realName = decodeURIComponent(fileName);
            const start = realName.lastIndexOf('.');
            return realName.substring(start + 1, realName.length).toLowerCase();
        }
        return '';
    };

    const handlePreview = () => {
        if (isMobile) {
            let path = file;
            if (['pdf', 'doc', 'docx'].includes(getFileType(file))) {
                path = `https://view.officeapps.live.com/op/view.aspx?src=${file}`;
                window.open(path, '_blank');
                return;
            }
        }
        const finalParam = {
            title,
            file
        };
        localStorage.setItem(
            'localDeclare',
            JSON.stringify({
                [id]: finalParam
            })
        );
        window.open(`${window.location.origin}/external/interview/register/declare?id=${id}`, '_blank');
        // history.push({
        //     pathname: '/external/interview/register/declare',
        //     state: { tabRoute: true },
        //     query: {
        //         id
        //     }
        // });
    };

    return (
        <>
            {getFieldDecorator('remove', {
                initialValue: false,
                validateTrigger: '',
                rules: [
                    {
                        // required: needBackgroundFlag === 1,
                        // message: '请先勾选同意声明',
                        validator(rule, value, callback) {
                            if (needBackgroundFlag === 0) {
                                callback(); // 无需背调的情况
                            } else if (needBackgroundFlag === 1 && value === false) {
                                callback('请先勾选同意声明');
                            } else if (value === false) {
                                callback('提交应聘登记表需勾选同意声明');
                            } else {
                                callback();
                            }
                        }
                    }
                ]
            })(<Declare title={title} file={file} onPreview={handlePreview} disabled={disabled} />)}
        </>
    );
};

export default DeclareInfo;
